<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/17/017
 * Time: 13:31
 */

namespace app\Lib\Model\Order;


use app\Lib\Exception\BlException;
use app\Lib\Model\DbModel\OrderInfo;
use app\Lib\Model\User\User;
use app\validate\OrderInfoValidate;

class AddOrder
{
    private $user;
    private $order_number;
    private $user_name;
    private $user_phone;
    private $user_address;
    private $order_money;
    private $order_style;
    private $house_type;
    private $introducer_phone;
    private $duration;
    private $user_sex;
    private $drawing_uid;
    private $design_uid;
    public function __construct(User $user)
    {
        $this->user = $user;
    }

    private function generateOrderNumber(){
        if(empty($this->order_number)){
            $this->order_number = date('Ym',time()).mt_rand(1000,9999);
        }
    }

    /**
     * 用户名称
     * @param mixed $user_name
     */
    public function setUserName($user_name)
    {
        $this->user_name = $user_name;
    }

    /**
     * 用户电话
     * @param mixed $user_phone
     */
    public function setUserPhone($user_phone)
    {
        $this->user_phone = $user_phone;
    }

    /**
     * 用户住址
     * @param mixed $user_address
     */
    public function setUserAddress($user_address)
    {
        $this->user_address = $user_address;
    }

    /**
     * 金钱
     * @param mixed $order_money
     */
    public function setOrderMoney($order_money)
    {
        $this->order_money = $order_money;
    }

    /**
     * 装修风格
     * @param mixed $order_style
     */
    public function setOrderStyle($order_style)
    {
        $this->order_style = $order_style;
    }

    /**
     * 户型
     * @param mixed $house_type
     */
    public function setHouseType($house_type)
    {
        $this->house_type = $house_type;
    }

    /**
     * 介绍人电话
     * @param mixed $introducer_phone
     */
    public function setIntroducerPhone($introducer_phone)
    {
        $this->introducer_phone = $introducer_phone;
    }

    /**
     * 工期
     * @param mixed $duration
     */
    public function setDuration($duration)
    {
        $this->duration = $duration;
    }

    /**
     * @param mixed $user_sex
     */
    public function setUserSex($user_sex)
    {
        $this->user_sex = $user_sex;
    }

    /**
     * @param mixed $drawing_uid
     */
    public function setDrawingUid($drawing_uid)
    {
        $this->drawing_uid = $drawing_uid;
    }

    public function setDesign_uid($design_uid){
        $this->design_uid = $design_uid;
    }

    /**
     *
     * @throws BlException
     */
    public function run(){
        if(!$this->user->getRoleClass()->isDesigner() && !$this->user->getRoleClass()->isSuperAdministrator()){
            throw new BlException('你无权建立工单');
        }
        $this->generateOrderNumber();
        $db = new OrderInfo();
        $data = [
            $db->_create_uid=>$this->user->getUid(),
            $db->_create_time=>time(),
            $db->_duration=>$this->duration,
            $db->_introducer_phone=>$this->introducer_phone,
            $db->_design_uid=>$this->design_uid,
            $db->_house_type=>$this->house_type,
            $db->_order_style=>$this->order_style,
            $db->_order_money=>$this->order_money,
            $db->_user_address=>$this->user_address,
            $db->_user_name=>$this->user_name,
            $db->_user_phone=>$this->user_phone,
            $db->_order_number=>$this->order_number,
            $db->_user_sex=>$this->user_sex,
            $db->_drawing_uid=>$this->drawing_uid,
        ];
        $check = (new OrderInfoValidate());
        if(!$check->scene('addOrder')->check($data)){
            throw new BlException($check->getError());
        }
        $result = $db->insert($data,false,true);
        if(!$result){
            throw new BlException('下单失败');
        }
        $orderNumber = Order::getOrderNumber($result);
        //推送业主
        $order = new Order($orderNumber);
        $orderPush = new OrderPush($order);
        //推送业主
        $orderPush->addOrder();
        //推送工程部经理
        $orderPush->pushAddOrder();
        return true;
    }
}